package com.wbs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbs.mapper.TbGoodsMapper;
import com.wbs.model.*;
import com.wbs.resutil.PageBean;
import com.wbs.resutil.PageResult;
import com.wbs.service.GoodsDescService;
import com.wbs.service.GoodsService;
import com.wbs.service.ItemService;
import com.wbs.vo.GoodsDescVO;
import com.wbs.vo.GoodsVO;
import com.wbs.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/1/31 18:14
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Autowired
    private GoodsDescService goodsDescService;

    @Autowired
    private ItemService itemService;

    //增加商品与商品描述
    @Override
    public void add(GoodsVO goodsVO) {
        //类型转换
        TbGoods tbGoods = this.conversionTb(goodsVO);
        tbGoodsMapper.insertSelective(tbGoods);

        //增加Goods
        //copy属性给GoodsDescVO
        GoodsDescVO goodsDescVO = new GoodsDescVO();
        BeanUtils.copyProperties(goodsVO.getGoodDescVO(),goodsDescVO);
        //获取tbGoods增加后的主键
        List<TbGoods> tbGoods1 = this.conditionFound(goodsVO);
        //获取最后一个TbGoods
        TbGoods tbGoods2 = tbGoods1.get(tbGoods1.size() - 1);
        goodsDescVO.setGoodsId(tbGoods2.getId());

        //增加GoodsDesc
        goodsDescService.add(goodsDescVO);
    }

    //分页查询全部商品信息
    @Override
    public PageResult<GoodsVO> findByConditionPage(PageBean<GoodsVO> pageBean) {
        // 设置分页参数
        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        // 模糊查询 调用查询条件 方法 buildExample()
        List<TbGoods> tbGoodsList = tbGoodsMapper.selectByExample(buildExample(pageBean.getBean()));

        PageInfo<TbGoods> pageInfo = new PageInfo<>(tbGoodsList);
        //实体类列表转换 List<TbTypeTemplate> -> List<TypeTemplateVo> 调佣类型列表转换方法convertToVoList()
        List<GoodsVO> collect = pageInfo.getList().stream().map(this::conversionVO).collect(Collectors.toList());
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), collect);
    }

    //条件查询
    private List<TbGoods> conditionFound(GoodsVO goodsVO){
        TbGoodsExample tbGoodsExample = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = tbGoodsExample.createCriteria();
        //商家ID
        if (!StrUtil.isEmpty(goodsVO.getSellerId())){
            criteria.andSellerIdEqualTo(goodsVO.getSellerId());
        }
        //商品名
        if (!StrUtil.isEmpty(goodsVO.getGoodsName())){
            criteria.andGoodsNameEqualTo(goodsVO.getGoodsName());
        }
        //状态
        if (!StrUtil.isEmpty(goodsVO.getAuditStatus())){
            criteria.andAuditStatusEqualTo(goodsVO.getAuditStatus());
        }
        //是否上架
        if (!StrUtil.isEmpty(goodsVO.getIsMarketable())){
            criteria.andIsMarketableEqualTo(goodsVO.getIsMarketable());
        }
        //品牌
        if (goodsVO.getBrandId()!=null&&goodsVO.getBrandId()!=0){
            criteria.andBrandIdEqualTo(goodsVO.getBrandId());
        }
        //是否启用规格
        if (!StrUtil.isEmpty(goodsVO.getIsEnableSpec())){
            criteria.andIsEnableSpecEqualTo(goodsVO.getIsEnableSpec());
        }
        return tbGoodsMapper.selectByExample(tbGoodsExample);
    }

    //提交审核
    @Override
    public void submitReview(Long id) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
        if ("0".equals(tbGoods.getAuditStatus())){
            tbGoods.setAuditStatus("1");
            tbGoodsMapper.updateByPrimaryKey(tbGoods);
        }

    }

    //审核通过/驳回
    @Override
    public void approvalOrReject(Long id, String auditStatus) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
        if ("1".equals(tbGoods.getAuditStatus())){
            tbGoods.setAuditStatus(auditStatus);
            tbGoodsMapper.updateByPrimaryKey(tbGoods);
        }
    }

    //按id查询 Goods 与 GoodsDesc
    @Override
    public GoodsVO selectByIdGoodsAndDesc(Long id) {
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
        if (tbGoods==null){
            return null;
        }
        GoodsVO goodsVO = this.conversionVO(tbGoods);
        GoodsDescVO goodsDescVO = goodsDescService.selectByPrimaryKey(id);
        goodsVO.setGoodDescVO(goodsDescVO);

        //查询当前goodid对应的所有sku信息
        TbItem tbItem = new TbItem();
        tbItem.setGoodsId(id);
        List<ItemVO> itemVOS = itemService.conditionFound(tbItem);

        //置入goodsVO中
        goodsVO.setItemVOS(itemVOS);

        return goodsVO;
    }

    /*辅助方法----------------------------------------------------------------------------------------------------*/
// 构建查询条件的辅助方法 通过商家名和状态 构建模糊查询条件
    private TbGoodsExample buildExample(GoodsVO goodsVO) {
        TbGoodsExample tbGoodsExample = new TbGoodsExample();
        if (!StrUtil.isEmpty(goodsVO.getGoodsName())){
            tbGoodsExample.createCriteria().andGoodsNameLike("%"+goodsVO.getGoodsName()+"%");
        }
        if (!StrUtil.isEmpty(goodsVO.getAuditStatus())) {
            tbGoodsExample.createCriteria().andAuditStatusLike("%" + goodsVO.getAuditStatus() + "%");
        }
        return tbGoodsExample;
    }

    //转换为Tb
    private TbGoods conversionTb(GoodsVO goodsVO){
        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVO,tbGoods);
        return tbGoods;
    }
    //转换为VO
    private GoodsVO conversionVO(TbGoods tbGoods){
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(tbGoods,goodsVO);
        return goodsVO;
    }
}
